/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Rick Dröge
 */

package cronus.client;
import java.awt.event.ActionEvent;
import javax.swing.JTabbedPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.*;

/**
 *
 * @author Rick
 */
public class InfoClientMainScreen extends JFrame implements ActionListener
{   
    
    private SocketConnection socketcon = new SocketConnection();
    
    private String query;
    private Table tablers = new Table(query);
    
    private File file = new File("C:\\temp.log");
    private File nfile = new File("C:\\number.log");
    
    private String[] column = {};
    private int number = 0;
    
    int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
    int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
            
    private JPanel panel;
    private JMenuBar menubar;
    private JMenu bestand;
    private JMenuItem exit;
    private JButton ophalen;
    private JComboBox dropdown;
    private JTabbedPane tabs;
    
    public InfoClientMainScreen()
    {
        
        //Paneel
        panel = new JPanel();
        panel.setLayout(null);
        
        //Menubar
        menubar = new JMenuBar();
        bestand = new JMenu("Bestand");
        menubar.add(bestand); 
        
        //Menu
        exit = new JMenuItem("Exit");
        bestand.add(exit);
        exit.addActionListener(this);
        
        //Buttons
        ophalen = new JButton("Ophalen");
        ophalen.setBounds(230, 15, 100, 30);
        
        //Tabs
        tabs = new JTabbedPane();
        tabs.setBounds(10, 40, 500, 400);
        
        //Dropdown menu's
        dropdown = new JComboBox();
        dropdown.setBounds(10, 15, 200, 20);
        
        
        String rapportquery = "SELECT rapportnaam From rapporten";
        socketcon.send(rapportquery);
        
        Object[][] rowresult = socketcon.read();
                
        for(Object[] ro : rowresult)
        {
            for(Object oa : ro)
            {
                dropdown.addItem(oa);
            }
        }
        
//        String columnnamesquery = "SELECT * FROM [INFORMATION_SCHEMA].[COLUMNS] where [TABLE_NAME] = '[CRONUS Nederland BV$Customer]'";
//        
//        socketcolumn.send(columnnamesquery);
//        
//        Object[][] columnresult = socketcolumn.read();
//        Object columns[] = new Object[columnresult.length];
//        
//        for(Object[] co : columnresult)
//        {
//            for(int i = 0; i < columns.length; i++)
//            {
//                columns[i] = co[i];
//            }
//        }
        String[] test = {"ABC", "Test", "DEF"};
        
        
        try
        {
            BufferedReader br = new BufferedReader(new FileReader(file));
            BufferedReader nbr = new BufferedReader(new FileReader(nfile));
            String sn =  nbr.readLine();
            
            number = Integer.parseInt(sn);
                        
            column = new String[number];
            
            for(int i = 0; i < number; i++)
            {
                column[i] = br.readLine();
                System.out.println("Kolom["+ i + "] = " + column[i]);
            }
            
            br.close();
            nbr.close();
            
            System.out.println("String nummer: " + sn);
            System.out.println("Int nummer: " + number);
            System.out.println("Kolom: " + column[0]);
        }
        catch(FileNotFoundException ex)
        {
            System.out.println("Het Systeem kan het opgegeven bestand niet vinden");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
        //Tabel
        final JTable table = new JTable(tablers.result, test);
        table.setPreferredScrollableViewportSize(new Dimension(500, 200));
        table.setFillsViewportHeight(true);
        
        JScrollPane tableview = new JScrollPane(table);
        tableview.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        tableview.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        //Voeg de tabel toe aan de tabs
        tabs.add("Tabel Weergave", tableview);
   
        //Content toevoegen aan het paneel
        panel.add(tabs);
        panel.add(dropdown);
        panel.add(ophalen);
        panel.add(menubar);
        
        setTitle("Info Client");
        setContentPane(panel);
        setJMenuBar(menubar);
        setSize(550, 550);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == exit)
        {
            this.setVisible(false);
            //loginscreen.setVisible(true);
        }
    }
}